package com.itqf.sms.service.impl;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itqf.sms.dto.CommonsDto;
import com.itqf.sms.mapper.UserMapper;
import com.itqf.sms.pojo.User;
import com.itqf.sms.service.UserService;
import com.itqf.sms.utils.StringUtils;
import com.itqf.sms.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.util.List;

/**
 * projectName: qk_javaproject
 *
 * @author: 肖成扬
 * time: 2021/9/8 14:52
 * description:用户模块实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public R login(User user) {
        //2.根据账号查询 进行数据库查询 -user
        if (user.getAccount()==null ||user.getAccount()=="") {
            return R.FAIL("账号不能为空");
        }
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount());
        //数据库查询
        User dbUser =userMapper.selectOne(queryWrapper);
        //3.如果user为null，证明账号错误
        if (dbUser==null) {
            return R.FAIL("账号错误");
        }
        //4.如果user不为null，判断deleted是否为1
        //if==1，逻辑删除
        if (dbUser.getDeleted()==1) {
            return R.FAIL("账号已经被删除，请联系管理员恢复");
        }
        //if不等1，比对密码
        //1.user中包含账号和密码(明文)
        //将密码进行md5加密+sms的盐

        String password= DigestUtils.md5DigestAsHex((user.getPassword()+"sms").getBytes());
        //5.根据密码对比结果返回R
        if (!password.equals(dbUser.getPassword())) {
            return R.FAIL("密码错误!");
        }
        return R.OK("登陆成功",dbUser);
    }

    @Override
    public R findUserList(String key) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",0);
        if(key!=null&&key!=" "){
            queryWrapper.like("name",key);
        }
        List<User> users = userMapper.selectList(queryWrapper);
        return R.OK("查询成功",users);

    }

    @Override
    public R removeUserById(int id) {
        User user=new User();
        user.setId(id);
        user.setDeleted(1);
        userMapper.updateById(user);
        int rows=userMapper.updateById(user);

        if (rows==0) {
            return R.FAIL("用户删除失败");
        }
        return R.OK("用户删除成功");
    }

    @Override
    public R saveUser(User user) {
        user.setPassword(StringUtils.dealPassword(user.getPassword()));
        int rows=0;
        try {
            rows=userMapper.insert(user);
        }catch (Exception e){

        }
        if(rows==0){
            return R.FAIL("数据格式错误，保存失败！");
        }
        return R.OK("用户信息保存成功！");
    }

    @Override
    public R updateUser(User user) {
        user.setAccount(null);
        user.setPassword(null);
        user.setDeleted(null);
        int rows=0;
        try {
            rows=userMapper.updateById(user);
        }catch (Exception e){

        }
        if(rows==0){
            return R.FAIL("数据格式错误，修改失败！");
        }
        return R.OK("用户信息修护改成功！");
    }

    @Override
    public R changeUser(CommonsDto commonsDto) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",commonsDto.getAccount());
        queryWrapper.eq("password", StringUtils.dealPassword(commonsDto.getOld()));
        User user = userMapper.selectOne(queryWrapper);
        if(user==null){
            return  R.FAIL("原密码错误");
        }
        user.setPassword(StringUtils.dealPassword(commonsDto.getNow()));
        user.setAccount(null);
        userMapper.updateById(user);
        return R.OK("密码修改成功");
    }

}
